package com.xdemo.core;

import java.io.FileReader;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.log4j.Logger;

public class ScriptManager {
	
	private final static Logger logger = Logger.getLogger(ScriptManager.class);
	
	private String scriptRoot;
	
	private ScriptEngine engine;
	
	public ScriptManager(String scriptRoot) {
		this.scriptRoot = scriptRoot;
		ScriptEngineManager manager = new ScriptEngineManager();
		engine = manager.getEngineByExtension("js");
	}
	
	public void runScript(String jsfile,Bindings bindings) {
		engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
		try {
			engine.eval(new FileReader(scriptRoot + "/" + jsfile));
		} catch (Exception e) {
			logger.error("service config read error!", e);
			System.exit(-1);
		}
	}
	
}
